<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <!-- basic styles -->
        <link href="../../assets/css/bootstrap.min.css" rel="stylesheet" />
        <link rel="stylesheet" href="../../assets/css/font-awesome.min.css" />
        <link rel="stylesheet" href="../../assets/css/ace.min.css" />
        <link rel="stylesheet" href="../../assets/css/ace-rtl.min.css" />
        <link rel="stylesheet" href="../../assets/css/ace-skins.min.css" />
    </head>
    <body>
        <div class="main-container">
            <div class="col-xs-12">
                <h3 class="header smaller lighter blue">用户组管理</h3>
                <div style="padding-bottom: 5px;">
                    <button class="btn btn-success" id="addGroup">添加用户组</button>
                </div>
                <div class="table-header">
                    默认库设置
                </div>
                <div class="table-responsive">
                    <table id="sample-table-2" class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>id</th>
                                <th>用户组名</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <script src="../../assets/js/jquery.js"></script>
        <script src="../../assets/js/bootstrap.min.js"></script>
        <!-- page specific plugin scripts -->
        <script src="../../assets/js/jquery-ui-1.10.3.custom.min.js"></script>
        <script src="../../assets/js/jquery.dataTables.min.js"></script>
        <script src="../../assets/js/jquery.dataTables.bootstrap.js"></script>
        <script src="../../assets/js/jquery.validate.js"></script>
        <script src="../../assets/js/jquery.validate-util.js"></script>
        <script src="../../assets/js/Base64.js"></script>
        <script src="logic.js"></script>
        <script type="text/javascript">
            var base64 = new Base64();
            //var type = getQueryString("type");
            //var DepotID = getQueryString("id");
            var DepotName = decodeURIComponent(getQueryString("name"));
//            DepotName = base64.decode(DepotName);
            jQuery(function ($) {
                listGroups();
                $("#addGroup").click(function () {
                    addGroupDialog();
                });
            });

            //添加用户组弹窗
            function addGroupDialog() {
                closeGroupDialog();
                var cont = '';
                cont += "<div id='modal-wizard' class='modal'>";
                cont += "            <div class='modal-dialog'>";
                cont += "                <div class='modal-content'>";
                cont += "                    <div class='modal-header' data-target='#modal-step-contents'>";
                cont += "                        <h3>用户组设置</h3>";
                cont += "                    </div>";
                cont += "                    <div class='modal-body step-content' id='modal-step-contents'>";
                cont += "                        <div class='' id='modal-step1'>";
                cont += "                            <div class='center'>";
                cont += "                                <form class='form-horizontal'>";
                cont += "                                    <div class='form-group'>";
                cont += "                                        <label for='Dev_Name' class='col-sm-3 control-label no-padding-right'>用户组名称</label>";
                cont += "                                        <div class='col-sm-9'>";
                cont += "                                            <input type='text' class='col-xs-8' id='name' name='1' required>";
                cont += "                                        </div>";
                cont += "                                    </div>";
                cont += "                                </form>";
                cont += "                            </div>";
                cont += "                        </div>";
                cont += "                    </div>";
                cont += "                    <div class='modal-footer wizard-actions'>";
                cont += "                        <button class='btn btn-success btn-sm btn-next save' data-last='完成 ' onclick='saveGroup()'>";
                cont += "                            完成";
                cont += "                            <i class='icon-ok'></i>";
                cont += "                        </button>";
                cont += "                        <button class='btn btn-danger btn-sm pull-left' data-dismiss='modal'>";
                cont += "                            <i class='icon-remove'></i>";
                cont += "                            取消";
                cont += "                        </button>";
                cont += "                    </div>";
                cont += "                </div>";
                cont += "            </div>";
                cont += "        </div>";
                $("body").append(cont);

                $('#modal-wizard').modal("show");
            }

            //关闭用户组弹窗
            function closeGroupDialog() {
                $('#modal-wizard').modal("hide");
                $('#modal-wizard').remove();
            }

            //保存用户组
            function saveGroup() {
                var form = $("#modal-step1 .form-horizontal").validate();
                if (form.form()) {
                    var name = $("#name").val();
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "../../action/dpium/group/create.do",
                        data: {groupName: base64.encode(name), instanceName: DepotName},
                        dataType: "json",
                        success: function (data) {
                            if (data.result) {
                                if (data.result) {
                                    alert("添加成功！");
                                }
                                ;
                                closeGroupDialog();
                                window.location.reload();
                            }
                        }
                    });
                }
            }

            function listGroups() {
                var oper = '';
                oper += "<a class='blue' href='javascript:void(0)' title='查看' '>";
                oper += "    <i class='icon-pencil bigger-130'></i>";
                oper += "</a>";
                oper += "<a class='red' href='javascript:void(0)' title='删除' '>";
                oper += "    <i class='icon-trash bigger-130'></i>";
                oper += "</a>";
                var oTable1 = $('.table').dataTable({
                    "aoColumns": [
                        {"bSortable": false},
                        {"bSortable": false},
                        {"bSortable": false, "sDefaultContent": oper}
                    ],
                    "bFilter": false,
                    "bSort": false,
                    "bProcessing": true,
                    "bServerSide": true,
                    // "sAjaxSource": "../../dpium/group/list?instanceName=" + DepotName,
                    "sAjaxSource": "../../action/dpium/group/list.do",
                    "sAjaxDataProp": "aaData",
                    fnServerData: function(sUrl, aoData, fnCallback, oSettings){
                        aoData.push({"name":"instanceName","value":DepotName});
                        oSettings.jqXHR = $.ajax({
                            "url": sUrl,
                            "data": aoData,
                            "success": function(data){
                                fnCallback(data);
                            },
                            "dataType": "json",
                            "cache": false
                        });
                    }
                });

                $(".table tbody").on('click', 'a', function () {
//                    var data =oTable1.row($(this).parents("tr").data());
//                    console.log($(this).parents("tr").find("td").first().html());
                    var id = $(this).parents("tr").find("td").first().html();
                    if ($(this).attr("title") === "查看") {
                        editGroupById(id);
                    }
                    if ($(this).attr("title") === "删除") {
                        delGroupById(id);
                    }

                });
            }

            function delGroupById(UserGroupNo) {
//                console.log("delete "+UserGroupNo);
                var r = confirm("确定删除该用户组吗?");
                if (r === true) {
                    $.ajax({
                        type: "GET",
                        async: false,
                        url: "../../action/dpium/group/delete.do",
                        data: {groupId: UserGroupNo},
                        dataType: "json",
                        success: function (resp) {
                            if (resp.result === "true") {
                                alert("删除成功！");
                            } else {
                                alert("删除失败！");
                            }
                            window.location.reload();
                        }
                    });
                }

            }

            function editGroupById(UserGroupNo) {
//                console.log("user-account.html" + window.location.search + "&gid=" + UserGroupNo);
                window.location = "user-account.html" + window.location.search + "&gid=" + UserGroupNo + "&instanceName=" + DepotName;
            }



        </script>
    </body>
</html>
